package edu.neu.agv_backend.controller;

import edu.neu.agv_backend.dto.*;
import edu.neu.agv_backend.service.FlawService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/agv/flaw")
public class FlawController {

    private final FlawService flawService;

    public FlawController(FlawService flawService) {
        this.flawService = flawService;
    }

    @GetMapping("/list")
    public TableDataInfo<AgvFlawDto> listFlaw(@RequestParam Map<String, String> params) {
        String query = params.isEmpty() ? "" : ("?" + params.entrySet().stream()
                .map(e -> e.getKey() + "=" + e.getValue())
                .collect(Collectors.joining("&")));
        return flawService.listFlaw(query);
    }

    @GetMapping("/{id}")
    public AjaxResult<AgvFlawDto> getFlaw(@PathVariable Long id) {
        return flawService.getFlaw(id);
    }

    @PostMapping
    public AjaxResult<Void> addFlaw(@RequestBody AgvFlawDto dto) {
        return flawService.addFlaw(dto);
    }

    @PutMapping
    public AjaxResult<Void> updateFlaw(@RequestBody AgvFlawDto dto) {
        return flawService.updateFlaw(dto);
    }

    @DeleteMapping("/{id}")
    public AjaxResult<Void> delFlaw(@PathVariable Long id) {
        return flawService.delFlaw(id);
    }

    @GetMapping("/live/{id}")
    public AjaxResult<Void> liveInfo(@PathVariable Long id) {
        return flawService.liveInfo(id);
    }

    @GetMapping("/check/{id}")
    public AjaxResult<Void> checkAllConfirmed(@PathVariable Long id) {
        return flawService.checkAllConfirmed(id);
    }
} 